<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList(1)">
      <el-form-item>
        <el-input v-model="dataForm.keyword.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
        <el-button v-if="isAuth('audio:pbaaudioitem:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('audio:pbaaudioitem:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="orders"
        header-align="center"
        align="center"
        width="70"
        label="序号">
      </el-table-column>
      <el-table-column
        prop="itemName"
        header-align="center"
        align="center"
        label="子项名称">
      </el-table-column>
      <el-table-column
        prop="coverImgA"
        header-align="center"
        width="90"
        align="center"
        label="封面图a-竖">
        <template slot-scope="scope">
          <img class="cover-img" :src="scope.row.coverImgA">
        </template>
      </el-table-column>
      <el-table-column
        prop="coverImgB"
        header-align="center"
        width="92"
        align="center"
        label="封面图b-横">
        <template slot-scope="scope">
          <img class="cover-img" :src="scope.row.coverImgB">
        </template>
      </el-table-column>
      <el-table-column
        prop="audioUrl"
        header-align="center"
        align="center"
        :show-overflow-tooltip="true"
        label="音频url">
      </el-table-column>
      <!-- -1上传失败，0处理中，1上传成功 -->
      <el-table-column
        prop="uploadStatus"
        header-align="center"
        align="center"
        label="上传状态">
        <template slot-scope="scope">
          {{scope.row.uploadStatus==1?'上传成功':(scope.row.uploadStatus==0?'处理中':'上传失败')}}
        </template>
      </el-table-column>
      <!-- 1上架,0下架,-1级联下架 -->
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="上下架状态">
        <template slot-scope="scope">
          {{scope.row.status==1?'上架':(scope.row.status==0?'下架':'级联下架')}}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="modifyStatus(scope.row.id,scope.row.status==0?'1':'0')">
            {{scope.row.status==1?'下架':'上架'}}
          </el-button>
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          <!-- <el-button type="text" size="small">
            <a download="" :href="download(scope.row.id)" style="text-decoration: none;color: #66b1ff;"
               v-if="scope.row.uploadStatus==1&&isAuth('audio:pbaaudioitem:download')">
              下载
            </a>
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './pbaaudioitem-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          keyword: {
            key: ''
            ,pId: ''
          }
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    created () {
      this.dataForm.keyword.pId = this.$route.query.id
      this.getDataList()
    },
    activated () {
      if (this.$route.query.id) {
        this.dataForm.keyword.pId = this.$route.query.id
        this.getDataList()
      }
    },
    methods: {
      // 获取数据列表
      getDataList (formSearch) {
        // 搜索框来源
        if (formSearch === 1) {
          this.pageIndex = 1
        }
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/audio/pbaaudioitem/list'),
          method: 'post',
          data: this.$http.adornData({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'keyword': this.dataForm.keyword
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, this.dataForm.keyword.pId)
        })
      },
      //下载
      download (id) {
        return this.$http.adornUrl(`/picbook/picbookitem/download/singleBookItem/${id}?token=${this.$cookie.get('token')}`)
      },
      //上下架
      modifyStatus (id, status) {
        this.$http({
          url: this.$http.adornUrl('/audio/pbaaudioitem/modifyStatus'),
          method: 'post',
          data: this.$http.adornData({
            'id': id,
            'status': status
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        // this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        this.$confirm(`确定[${id ? '删除' : '批量删除'}]?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/audio/pbaaudioitem/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style lang="less" scoped>
  /deep/ .el-table--medium td .cell {
    padding: 0;
  }
  .el-table--medium td .cell .cover-img {
    padding: 0px;
    width: 50px;
    height: 70px;
  }
</style>